package cn.messageplus.core.test.server.controller;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author mo
 * @日期: 2025-01-13 16:01
 **/
@RestController
@RequestMapping("/login")
public class LoginController {
    private static final Map<String, String> accountMap = new ConcurrentHashMap<>();
    static {
        accountMap.put("admin", "admin");
        accountMap.put("张三", "123456");
        accountMap.put("lisi", "123456");
    }

    @GetMapping
    public boolean login(String username, String password) {
        String pass = accountMap.get(username);
        if (pass == null) {
            return false;
        } else {
            if (pass.equals(pass)) {
                StpUtil.login(username, password);
                return true;
            } else {
                return false;
            }
        }
    }
    @GetMapping("/is")
    public String isLogin() {
        try {
            return StpUtil.getLoginIdAsString();
        } catch (NotLoginException e) {
            return "";
        }
    }
}
